(function() {
    var e = "id" in document ? document.id : window.$;
    en4 = {};
    en4.activity = {
        load: function(t, n) {
            if (en4.core.request.isRequestActive()) return;
            e("feed_viewmore").style.display = "none";
            e("feed_loading").style.display = "";
            en4.core.request.send(new Request.HTML({
                url: en4.core.baseUrl + "activity/widget/feed",
                data: {
                    maxid: t,
                    feedOnly: true,
                    nolayout: true,
                    subject: n
                }
            }), {
                element: e("activity-feed"),
                updateHtmlMode: "append"
            })
        },
        like: function(t, n) {
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/index/like",
                data: {
                    format: "json",
                    action_id: t,
                    comment_id: n,
                    subject: en4.core.subject.guid
                }
            }), {
                element: e("activity-item-" + t),
                updateHtmlMode: "comments"
            })
        },
        unlike: function(t, n) {
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/index/unlike",
                data: {
                    format: "json",
                    action_id: t,
                    comment_id: n,
                    subject: en4.core.subject.guid
                }
            }), {
                element: e("activity-item-" + t),
                updateHtmlMode: "comments"
            })
        },
        comment: function(t, n) {
            if (n.trim() == "") {
                return
            }
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/index/comment",
                data: {
                    format: "json",
                    action_id: t,
                    body: n,
                    subject: en4.core.subject.guid
                }
            }), {
                element: e("activity-item-" + t),
                updateHtmlMode: "comments"
            })
        },
        attachComment: function(e) {
            var t = this;
            e.addEvent("submit", function(n) {
                n.stop();
                t.comment(e.action_id.value, e.body.value)
            })
        },
        viewComments: function(t) {
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/index/viewComment",
                data: {
                    format: "json",
                    action_id: t,
                    nolist: true
                }
            }), {
                element: e("activity-item-" + t),
                updateHtmlMode: "comments"
            })
        },
        viewLikes: function(t) {
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/index/viewLike",
                data: {
                    format: "json",
                    action_id: t,
                    nolist: true
                }
            }), {
                element: e("activity-item-" + t),
                updateHtmlMode: "comments"
            })
        },
        hideNotifications: function(t) {
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/notifications/hide"
            }));
            if (e("notifications_main")) {
                var n = e("notifications_main").getChildren("li");
                n.each(function(e) {
                    e.setAttribute("class", "")
                })
            }
            if (e("notifications_menu")) {
                var n = e("notifications_menu").getChildren("li");
                n.each(function(e) {
                    e.setAttribute("class", "")
                })
            }
        },
        updateNotifications: function() {
            if (en4.core.request.isRequestActive()) return;
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/notifications/update",
                data: {
                    format: "json"
                },
                onSuccess: this.showNotifications.bind(this)
            }))
        },
        showNotifications: function(e) {
            if (e.notificationCount > 0) {}
        },
        markRead: function(e) {
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/notifications/test",
                data: {
                    format: "json",
                    actionid: e
                }
            }))
        },
        cometNotify: function(t) {
            e("core_menu_mini_menu_updates").style.display = "";
            e("core_menu_mini_menu_updates_count").innerHTML = t.text
        }
    };

    NotificationUpdateHandler = function(e) {

        // Implements: [Events, Options],

        this.options = {
            debug: false,
            baseUrl: "/",
            identity: false,
            delay: 5e3,
            admin: false,
            idleTimeout: 6e5,
            last_id: 0,
            subject_guid: null
        },
        this.state = true,
        this.activestate = 1,
        this.fresh = true,
        this.lastEventTime = false,
        this.title = document.title,
        this.initialize = function(e) {
            this.setOptions(e)
        },
        this.start = function() {
            this.state = true;
            this.idleWatcher = new IdleWatcher(this, {
                timeout: this.options.idleTimeout
            });
            this.idleWatcher.register();
            this.addEvents({
                onStateActive: function() {
                    this.activestate = 1;
                    this.state = true
                }.bind(this),
                onStateIdle: function() {
                    this.activestate = 0;
                    this.state = false
                }.bind(this)
            });
            this.loop()
        },
        this.stop = function() {
            this.state = false
        },
        this.updateNotifications = function() {
            if (en4.core.request.isRequestActive()) return;
            en4.core.request.send(new Request.JSON({
                url: en4.core.baseUrl + "activity/notifications/update",
                data: {
                    format: "json"
                },
                onSuccess: this.showNotifications.bind(this)
            }))
        },
        this.showNotifications = function(e) {
            if (e.notificationCount > 0) {}
        },
        this.loop = function() {
            if (!this.state) {
                this.loop.delay(this.options.delay, this);
                return
            }
            try {
                this.updateNotifications().addEvent("complete", function() {
                    this.loop.delay(this.options.delay, this)
                }.bind(this))
            } catch (e) {
                this.loop.delay(this.options.delay, this);
                this._log(e)
            }
        },
        this._log = function(e) {
            if (!this.options.debug) {
                return
            }
            try {
                if (typeof console && $type(console)) {
                    console.log(e)
                }
            } catch (t) {}
        }
    }
    en4.activity.compose = {
        composers: {},
        register: function(e) {
            name = e.getName();
            this.composers[name] = e
        },
        deactivate: function() {
            for (var e in this.composers) {
                this.composers[e].deactivate()
            }
            return this
        }
    };
    en4.activity.compose.icompose = function(t, n) {
        // Implements: [Events, Options],

        this.name = false,
        this.element = false,
        // options: {},
        this.initialize = function(t, n) {
            this.element = e(t);
            this.setOptions(n)
        },
        this.getName = function() {
            return this.name
        },
        this.activate = function() {
            en4.activity.compose.deactivate()
        },
        this.deactivate = function() {}

    }
    ActivityUpdateHandler = function(e) {
        // Implements: [Events, Options],
        this.options = {
            debug: true,
            baseUrl: "/",
            identity: false,
            delay: 5e3,
            admin: false,
            idleTimeout: 6e5,
            last_id: 0,
            next_id: null,
            subject_guid: null,
            showImmediately: false
        },
        this.state = true,
        this.activestate = 1,
        this.fresh = true,
        this.lastEventTime = false,
        this.title = document.title,
        this.initialize = function(e) {
            this.setOptions(e)
        },
        this.start = function() {
            this.state = true;
            this.idleWatcher = new IdleWatcher(this, {
                timeout: this.options.idleTimeout
            });
            this.idleWatcher.register();
            this.addEvents({
                onStateActive: function() {
                    this._log("activity loop onStateActive");
                    this.activestate = 1;
                    this.state = true
                }.bind(this),
                onStateIdle: function() {
                    this._log("activity loop onStateIdle");
                    this.activestate = 0;
                    this.state = false
                }.bind(this)
            });
            this.loop()
        },
        this.stop = function() {
            this.state = false
        },
        this.checkFeedUpdate = function(t, n) {
            if (en4.core.request.isRequestActive()) return;
            var r = new Request.HTML({
                url: en4.core.baseUrl + "widget/index/name/activity.feed",
                data: {
                    format: "html",
                    minid: this.options.last_id + 1,
                    feedOnly: true,
                    nolayout: true,
                    subject: this.options.subject_guid,
                    checkUpdate: true
                }
            });
            en4.core.request.send(r, {
                element: e("feed-update")
            });
            r.addEvent("complete", function() {
                (function() {
                    if (this.options.showImmediately && e("feed-update").getChildren().length > 0) {
                        e("feed-update").setStyle("display", "none");
                        e("feed-update").empty();
                        this.getFeedUpdate(this.options.next_id)
                    }
                }).delay(50, this)
            }.bind(this));
            return r
        },
        this.getFeedUpdate = function(t) {
            if (en4.core.request.isRequestActive()) return;
            var n = this.options.last_id + 1;
            this.options.last_id = t;
            document.title = this.title;
            var r = new Request.HTML({
                url: en4.core.baseUrl + "widget/index/name/activity.feed",
                data: {
                    format: "html",
                    minid: n,
                    feedOnly: true,
                    nolayout: true,
                    getUpdate: true,
                    subject: this.options.subject_guid
                }
            });
            en4.core.request.send(r, {
                element: e("activity-feed"),
                updateHtmlMode: "prepend"
            });
            return r
        },
        this.loop = function() {
            this._log("activity update loop start");
            if (!this.state) {
                this.loop.delay(this.options.delay, this);
                return
            }
            try {
                this.checkFeedUpdate().addEvent("complete", function() {
                    try {
                        this._log("activity loop req complete");
                        this.loop.delay(this.options.delay, this)
                    } catch (e) {
                        this.loop.delay(this.options.delay, this);
                        this._log(e)
                    }
                }.bind(this))
            } catch (e) {
                this.loop.delay(this.options.delay, this);
                this._log(e)
            }
            this._log("activity update loop stop")
        },
        this._log = function(e) {
            if (!this.options.debug) {
                return
            }
            try {
                if ("console" in window && typeof console && "log" in console) {
                    console.log(e)
                }
            } catch (t) {}
        }
    }
})()